import compressing from "compressing";
import fs from "fs";
import path from "path";

const initOpts = {
    basePath: "./",
    archiverName: "dist.zip",
    sourceName: "dist",
};
export interface compressDestOption {
    basePath?: string;
    archiverName?: string;
    sourceName?: string;
}
function compressDest(opts?: compressDestOption | undefined) {
    const { sourceName, archiverName, basePath = '' } = opts || initOpts;
    return {
        name: "compress-dest",
        closeBundle() {
            console.log("closeBundle");

            const handleError = (err: any) => {
                throw err;
            };
            let sourcePath = path.join(__dirname,basePath + sourceName);
            let archiverPath = path.join(__dirname, basePath + archiverName);

            compressing.zip
                .compressDir(sourcePath, archiverPath, {
                    ignoreBase: true,
                })
                .then(() => {
                    console.log(
                        `compressing finish:${sourcePath} to ${archiverPath}`
                    );
                    fs.rename(
                        archiverPath,
                        sourcePath+'/' + archiverName,
                        (err) => {
                            if (err) {
                                throw err;
                            }
                            console.log(`remove finish`);
                        }
                    );
                })
                .catch(handleError);
        },
    };
}
export default compressDest;

